// Calculation_Abilities.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "Calculation_Lua.h"
#include "LuaInterface.h"
#include "Charplandoc.h"


// CCalculation_Lua-Dialogfeld

IMPLEMENT_DYNCREATE(CCalculation_Lua, CCalculationPage)

CCalculation_Lua::CCalculation_Lua(CWnd* pParent /*=NULL*/)
        : CCalculationPage(pParent)
{
}

// for runtime creations
BOOL CCalculation_Lua::Create(CWnd* pParentWnd)
{
    return CDialog::Create(CCalculation_Lua::IDD,pParentWnd);
}

CCalculation_Lua::~CCalculation_Lua()
{
}

void CCalculation_Lua::DoDataExchange(CDataExchange* pDX)
{
    CCalculationPage::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST1, mwnd_log);
}


BEGIN_MESSAGE_MAP(CCalculation_Lua, CCalculationPage)
    ON_MESSAGE(WM_LUA_ERROR, &CCalculation_Lua::OnLuaError)
    ON_BN_CLICKED(IDC_BUTTON1, &CCalculation_Lua::OnBnClickedButton1)
END_MESSAGE_MAP()


LRESULT CCalculation_Lua::OnLuaError(WPARAM, LPARAM)
{
    CLuaInterface* pl = GetDocument()->p_lua;
    if (!pl) return 0;

    const CStringArray& c = pl->GetLog();
    CDC*  pDC = mwnd_log.GetDC();

    int dx = mwnd_log.GetHorizontalExtent();
    CSize  sz;

    // refill if size is lower
    if (c.GetCount()<mwnd_log.GetCount())
    {
        dx = 0;
        mwnd_log.ResetContent();
    }

    // add new entries
    for (INT_PTR t=mwnd_log.GetCount();t<c.GetCount();++t)
    {
        CString str = c[t];
        sz = pDC->GetTextExtent(str);
        if (sz.cx > dx) dx = sz.cx;

        mwnd_log.AddString(str);
    }
    mwnd_log.SetTopIndex(mwnd_log.GetCount()-1);

    mwnd_log.ReleaseDC(pDC);
    mwnd_log.SetHorizontalExtent(dx);

    return 1;
}


BOOL CCalculation_Lua::OnInitDialog()
{
    CCalculationPage::OnInitDialog();

    CLuaInterface* pl = GetDocument()->p_lua;
    if (!pl) return 0;

    if (pl->IsInDebugMode()) OnLuaError(0,NULL);

    return TRUE;
}


void CCalculation_Lua::OnBnClickedButton1()
{
    GetDocument()->p_lua->Init();
    GetDocument()->p_lua->OnClassChanged(false);
    GetDocument()->p_lua->OnLevelChanged(false);
    GetDocument()->p_lua->OnUserOptionChanged(false);
    GetDocument()->p_lua->OnSpecChanged(false);
    GetDocument()->p_lua->OnRAChanged(false);
    GetDocument()->p_lua->OnChampChanged();
}
